//生成菜单
var menuItem = Vue.extend({
    name: 'menu-item',
    props:{item:{}},
    template:[
        '<li>',
        '	<a v-if="item.type === 0" href="javascript:;">',
        '		<i v-if="item.icon != null" :class="item.icon"></i>',
        '		<span  class="hide-menu">{{item.name}}</span>', 
        '	</a>',
        '	<ul v-if="item.type === 0" class="treeview-menu">',
        '		<menu-item :item="item" v-for="item in item.list"></menu-item>',
        '	</ul>',

        '	<a v-if="item.type === 1 && item.parentId === 0" :href="\'#\'+item.url" >',
        '		<i v-if="item.icon != null" :class="item.icon"></i>',
        '		<span  class="hide-menu">{{item.name}}</span>',
        '	</a>',

        '	<a v-if="item.type === 1 && item.parentId != 0"  :href="\'#\'+item.url"><i v-if="item.icon != null" :class="item.icon"></i><i v-else class="fa fa-circle-o"></i> {{item.name}}</a>',
        '</li>'
    ].join('')
});

//iframe自适应
$(window).on('resize', function() {
	var $content = $('.content');
	$content.height($(this).height() - 120);
	$content.find('iframe').each(function() {
		$(this).height($content.height());
	});
}).resize();

//注册菜单组件
Vue.component('menuItem',menuItem);

var vm = new Vue({
	el:'#myapp',
	data:{
        shojmu:false,
		user:{},
		menuList:{},
		main:"main.html",
		password:'',
		newPassword:'',
        navTitle:"首页",
        course:[],
        planClass:[],
        pageinfo:new page({}),
		pi:null,
        pageShow:false
	},
	methods: {
		getMenuList: function (event) {
			$.getJSON((window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"")+"sys/menu/nav?_"+$.now(), function(r){
				vm.menuList = r.menuList;
			});
		},
		getUser: function(){
			$.getJSON((window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"")+"sys/user/info?_"+$.now(), function(r){
				vm.user = r.user;
			});
		},
		getData:function(currentPage,cid){
			$.post((window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"")+"studentManage/getCourse",{pageIndex:currentPage,cId:cid},function (r) {
                vm.course=r.page.records;
            },'json');
		},
		//读取课程
		getCourse:function(){
			$.getJSON((window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"")+"studentManage/main?_"+$.now(),function (r) {
				vm.course=r.page.records;
				vm.planClass=r.planClass;
                if(r.page.pages>1){
                    vm.pageinfo=new page({
                        currentPage:1,
                        allPage:r.page.pages
                    });
                    vm.pageShow=true;
				}
			})
		},
		//查看章节
		goToChapter:function(ele,item){
            window.open((window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"")+"modules/plan/coursechapter.html?id="+item.id,"_blank");
		},
		//根据分类id查询课程
		searchCourseByClass:function(ele,item){
            vm.getData(1,item.id);
		},
        goPage:function(index){
			if(index==vm.pageinfo.currentPage) return;
            vm.pageinfo.mothed.go(index);
            vm.pi=index;
            vm.getData(index);
		},
		privae:function(flag){
            if(vm.pageinfo.currentPage==1) return;
            vm.pageinfo.mothed.prev();
            vm.pi=flag;
            vm.getData(vm.pageinfo.currentPage);
		},
		next:function(flag){
            if(vm.pageinfo.currentPage==vm.pageinfo.allPage) return;
            vm.pageinfo.mothed.next();
            vm.pi=flag;
            vm.getData(vm.pageinfo.currentPage);
		},
        addClass:function(index){
            this.current=index;
        },
		updatePassword: function(){
			layer.open({
				type: 1,
				skin: 'layui-layer-molv',
				title: "修改密码",
				area: ['550px', '270px'],
				shadeClose: false,
				content: jQuery("#passwordLayer"),
				btn: ['修改','取消'],
				btn1: function (index) {
					var data = "password="+vm.password+"&newPassword="+vm.newPassword;
					$.ajax({
						type: "POST",
					    url: (window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"")+"sys/user/password",
					    data: data,
					    dataType: "json",
					    success: function(result){
							if(result.code == 0){
								layer.close(index);
								layer.alert('修改成功', function(index){
									location.reload();
								});
							}else{
								layer.alert(result.msg);
							}
						}
					});
	            }
			});
		},
        donate: function () {
            layer.open({
                type: 2,
                title: false,
                area: ['806px', '467px'],
                closeBtn: 1,
                shadeClose: false,
                content: ['http://cdn.renren.io/donate.jpg', 'no']
            });
        }
	},
	created: function(){
		this.getMenuList();
		this.getUser();
	},
	updated: function(){
		//路由
		var router = new Router();
		routerList(router, vm.menuList);
		router.start();
	}
});

var oldurl = "";
function routerList(router, menuList){
	 
	for(var key in menuList){
		var menu = menuList[key];
		if(menu.type == 0){
			routerList(router, menu.list);
		}else if(menu.type == 1){
			router.add('#'+menu.url, function() {
				var url = window.location.hash;
				
				//替换iframe的url
			    vm.main = url.replace('#', '');
 
			    //导航菜单展开  --有TODO bug
			    $("a[href='"+oldurl+"']").removeClass("active");
			    oldurl = url;
			     
			    $("a[href='"+url+"']").addClass("active");
			    
			    vm.navTitle = $("a[href='"+url+"']").text();
			});
		}
	}
}
